package me.raih.service.impl;

import me.raih.pojo.vo.MyHost;
import me.raih.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @Author Raih
 * @Time 2021/3/4  16:20
 */
@Service
public class MailServiceImpl implements MailService {

    @Autowired
    MyHost myHost;

    @Autowired
    JavaMailSenderImpl mailSender;

    @Override
    public boolean sendSimEmail(String username,String sendTo,String checkCode) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setSubject("感谢使用本系统，这是你的注册验证信息");

            String text = "您账号 "+username+" 的注册码是<a href='http://"+myHost.getAddress()+":"+myHost.getPort()+"/user/emailCheck?checkCode="+checkCode+"&username="+username+"'"+">点我进行验证</a>";
            //开启HTML解析
            helper.setText(text,true);
            helper.setFrom(myHost.getUsername());
            helper.setTo(sendTo);
            //发送邮件
            mailSender.send(mimeMessage);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return false;
    }
}
